
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Represents a static list of the groups's members.
	''' </summary>
	Public Class GroupsUsers
		Inherits BaseList
		Protected groupID As Integer

		Public Sub New(ByVal grp As Group)
			Init(grp.ID)
		End Sub

		Public Sub New(ByVal groupID As Integer)
			Init(groupID)
		End Sub


		'
		' Private and protected methods
		'

		Protected Sub Init(ByVal groupID As Integer)
			Me.rnd = New Random()
			Me.groupID = groupID
			Me.list = Nothing
		End Sub

		Protected Overloads Overrides Function LoadList() As Hashtable
			Return LoadList(groupID)
		End Function

		Protected Function LoadList(ByVal groupID As Integer) As Hashtable
			Dim usersList As New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As SqlCommand = GetCommand(conn)
				Dim dr As SqlDataReader = cmd.ExecuteReader()
				While dr.Read()
					Dim usr As New GroupMember(dr)
					usersList.Add(usr.GetHashCode(), usr)
				End While
				dr.Close()
			Finally
				conn.Close()
			End Try

			Return usersList
		End Function

		Protected Overloads Overrides Sub ClearList()
			Throw New Exception("Can not use the user list object to manage group membership. Use the Group object instead.")
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal fUID As Integer)
			Throw New Exception("Can not use the user list object to manage group membership. Use the Group object instead.")
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal item As Object)
			Throw New Exception("Can not use the user list object to manage group membership. Use the Group object instead.")
		End Sub

		Protected Overloads Overrides Function AddItem(ByVal item As Object) As Boolean
			Throw New Exception("Can not use the user list object to manage group membership. Use the Group object instead.")
		End Function

		Protected Overridable Function GetCommand(ByVal conn As SqlConnection) As SqlCommand
			Dim cmd As New SqlCommand("SELECT * FROM GroupMembers WHERE GroupID=@GroupID AND Status=" + DirectCast(GroupMember.JoinStatus.jsAccepted, Integer))
			cmd.Connection = conn
			cmd.Parameters.Add("@GroupID", groupID)
			Return cmd
		End Function
	End Class
End Namespace






